<?php

namespace App\Module\Api\Controllers;
use App\Exceptions\Customize\ParamsException;
use App\Module\Api\Controllers\Request\TransferSubmitRequest;
use App\Module\Api\Validator\TransferSubmitValidator;
use App\System\Base\Services\SmsService;
use App\System\Base\Services\TokenService;
use App\System\Cis\Repositories\CisUserRepository;
use App\System\Fina\Repositories\FinaIntegralRegRecordRepository;
use App\System\Log\Repositories\LogNumberChangeRepository;
use Illuminate\Support\Facades\DB;

/**
 *账号注册币转移
 */
class TransferSubmitController extends Controller
{

    public function __construct()
    {
        $this->query = new TransferSubmitRequest();
        parent::__construct();

    }

    public function index()
    {
        (new TransferSubmitValidator())->check();
        DB::beginTransaction();
        $user_id = TokenService::getUserId();
        $master_id = TokenService::getMasterId();
        $user = CisUserRepository::getInstance();
        $master_info = $user->checkPayPassword($master_id,$this->query->payPassword);
        if(!$master_info)
        {
            throw new ParamsException([
                'error_code' => '1000',
                'message'    => '操作失败，支付密码错误！',
            ]);
        }
        if($this->query->action == 1)
        {//转移注册币
            if($user_id == $this->query->receiverId)
            {
                throw new ParamsException([
                    'error_code' => '1000',
                    'message'    => '操作失败，不能自己转给自己！',
                ]);
            }
            $receiver_info = $user->getUser($this->query->receiverId);
            if(!$receiver_info)
            {
                throw new ParamsException([
                    'error_code' => '1000',
                    'message'    => '接收账户不存在',
                ]);
            }
            $user_info = $user->getUser($user_id);
            if($user_info->registered_currency < $this->query->amount)
            {
                throw new ParamsException([
                    'error_code' => '1000',
                    'message'    => '账号注册币不足',
                ]);
            }
            $res_one = $user->updateUserPurse($user_id,2,'registered_currency',$this->query->amount);
            $res_two = $user->updateUserPurse($this->query->receiverId,1,'registered_currency',$this->query->amount);
            $pub_fina_set = ['record_type'=>2,'cash'=>$this->query->amount,'status'=>1,'commission'=>0];
            $fina = FinaIntegralRegRecordRepository::getInstance();
            $fins_res_one = $fina->createFina(array_merge($pub_fina_set,['income'=>2,'type'=>4,'after_cash'=>$user_info->registered_currency - $this->query->amount,'user_id'=>$user_id]));
            $fins_res_two = $fina->createFina(array_merge($pub_fina_set,['income'=>1,'type'=>3,'after_cash'=>$receiver_info->registered_currency + $this->query->amount,'user_id'=>$receiver_info->id]));
            if(!$res_one || !$res_two || !$fins_res_one || !$fins_res_two)
            {
                throw new ParamsException([
                    'error_code' => '1000',
                    'message'    => '操作失败，服务器内部错误',
                ]);
            }
        }
        else
        {//转移帐号
            if($master_info->mobile == $this->query->mobile)
            {
                throw new ParamsException([
                    'error_code' => '1000',
                    'message'    => '操作失败，不能自己转给自己！',
                ]);
            }
            $receiver_info = $user->getMasterUser($this->query->mobile);//查询接收帐号是否存在
            if(!$receiver_info)
            {
                throw new ParamsException([
                    'error_code' => '1000',
                    'message'    => '接收账户不存在',
                ]);
            }
            $res_one = $user->updateUser(['parent_id'=>$receiver_info->id,'mobile'=>$receiver_info->mobile],$user_id);//更新主账号所属人
            $log = LogNumberChangeRepository::getInstance();
            $res_two = $log->addData(['transferor_id'=>$master_id,'transferor_son_id'=>$user_id,'receiver_id'=>$receiver_info->id]);//添加志记录
            if(!$res_one || !$res_two)
            {
                throw new ParamsException([
                    'error_code' => '1000',
                    'message'    => '转移失败,服务器内部错误',
                ]);
            }
            TokenService::clearSunUserId();
            $sms = SmsService::getInstance();
            $sms->sendMsg($receiver_info->mobile,10,[$master_info->mobile,$user_id]);//发送短信
        }
        DB::commit();
        return apiResponse();
    }
}
